<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>map数据结构</title>
</head>
<body>
    <div></div>
    <script>
        //1、声明一个map
        let m = new Map();

        //2、添加元素
        m.set('name','商信');
        m.set('fn',function(){
            console.log('hello world')
        });
        let key = {
            game:'王者'
        }
        m.set(key,['王昭君','李元芳']);

        console.log(m)
        console.log(m.size)//3

        //3、读取键值
        let m3 = new Map();

        let hello = function() {console.log('hello');};
        m3.set(hello, 'Hello ES6!') // 键是函数

        console.log(m3.get(hello))
        console.log(m3.get('name'))//undefined

        //4、has 表示某个键是否在当前 Map 对象之中。
        let m4 = new Map();
        m4.set('edition', 6);
        m4.set(262, 'standard');
        console.log(m4.has('edition'))     // true
        console.log(m4.has('years'))       // false
        console.log(m4.has(262))           // true

        //5、delete方法删除某个键，返回true。如果删除失败，返回false。
        let m5 = new Map();
        m5.set('name', 'es6');
        m5.set(262, 'number');
       console.log(m5.delete('name'))//true
       console.log(m5.delete(123))//false

       //6、clear方法清除所有成员，没有返回值。
       let map6 = new Map();
       map6.set('foo', true);
       map6.set('bar', false);

       map6.size // 2
       map6.clear()
       map6.size // 0
       console.log(map6)

       //7、遍历方法for of
       //Map.prototype.keys()：返回键名的遍历器。
      //Map.prototype.values()：返回键值的遍历器
       const map7 = new Map([
            ['F', 'no'],
            ['T', 'yes'],
        ]);
        console.log(map7)
        for (let obj of map7) {
            console.log(obj);
        }
        for (let [key, value] of map7) {
            console.log(key, value);
        }
        for (let key of map7.keys()) {
            console.log(key);
        }
        // "F"
        // "T"
        for (let value of map7.values()) {
            console.log(value);
        }
        // "no"
        // "yes"
    </script>
</body>
</html>